package edu.gyc.hiblog.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gyc.hiblog.dao.PostDao;
import edu.gyc.hiblog.dao.UserMessageDao;
import edu.gyc.hiblog.model.Post;
import edu.gyc.hiblog.model.User;
import edu.gyc.hiblog.model.UserMessage;
import edu.gyc.hiblog.service.PostService;
import edu.gyc.hiblog.service.UserMessageService;
import edu.gyc.hiblog.service.UserService;
import edu.gyc.hiblog.shiro.AccountProfile;
import edu.gyc.hiblog.util.Result;
import edu.gyc.hiblog.vo.PostVo;
import edu.gyc.hiblog.vo.UserMessageVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller

public class UserController extends BaseController {
    @Autowired
    UserService userService;

    @Autowired
    PostService postService;

    @RequestMapping("/user/home")
    public String home(Model model) {
        AccountProfile accountProfile = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        User user = userService.getById(accountProfile.getId());

        List<Post> posts = postService.lambdaQuery().eq(Post::getUserId, user.getId())
                .gt(Post::getCreated, DateUtil.offsetDay(new Date(), -30))
                .orderByDesc(Post::getCreated)
                .list();


        model.addAttribute("posts", posts);
        model.addAttribute("user", user);
        return "user/home";
    }

    @RequestMapping("/user/set")
    public String set(Model model) {
        return "user/set";
    }

    @RequestMapping("/user/index")
    public String index(Model model) {
        return "user/index";
    }

    @ResponseBody
    @RequestMapping("/user/public")
    public Result userPublic(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                             Model model) {
        AccountProfile accountProfile = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        PageInfo<PostVo> postVos = postService.paging(pageNum, 2, null, accountProfile.getId(),
                null, null, "created");

        return Result.success(postVos);
    }

    @Autowired
    PostDao postDao;

    @ResponseBody
    @RequestMapping("/user/collection")
    public Result usercollection(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum
    ) {
        AccountProfile accountProfile = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        PageHelper.startPage(pageNum, 2);
        List<Post> posts = postDao.queryCollectionByUserId(accountProfile.getId());
        PageInfo<Post> pageData = new PageInfo<>(posts);
        return Result.success(pageData);
    }

    @Autowired
    UserMessageDao userMessageDao;

    //自定义属性，设置每页显示数据量
    @Value("${pageSize}")
    Integer pageSize;

    @RequestMapping("/user/message")
    public String message(Integer pageNum, Model model) {
        if (pageNum == null) {
            pageNum = 1;
        }
        AccountProfile accountProfile = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        PageHelper.startPage(pageNum, pageSize);
        List<UserMessageVo> userMessageVos = userMessageDao.queryUserMessage(accountProfile.getId());

        PageInfo<UserMessageVo> data = new PageInfo<>(userMessageVos);
        model.addAttribute("pageData", data);
        return "user/message";
    }

    @Autowired
    UserMessageService messageService;
    @PostMapping("/message/remove")
    public Result removemessage(Integer id, @RequestParam(defaultValue = "false") Boolean all) {
        AccountProfile accountProfile = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        if (id != null) {
            messageService.removeById(id);
        }else if(all){
            messageService.lambdaUpdate().eq(UserMessage::getToUserId,accountProfile.getId())
                    .remove();
        }


       return Result.success("删除消息成功");

    }
}
